{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Trained model evaluation\n",
    "\n",
    "Compute test set accuracy using the trained model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from keras.utils import np_utils\n",
    "from keras.datasets import cifar10\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "from keras import optimizers\n",
    "from keras.callbacks import LearningRateScheduler, CSVLogger, ModelCheckpoint\n",
    "import keras.backend as K\n",
    "from keras.backend import tensorflow_backend\n",
    "import tensorflow as tf\n",
    "\n",
    "from model.SEResNeXt import SEResNeXt\n",
    "from utils.img_util import arr_resize\n",
    "\n",
    "import json\n",
    "import configparser\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Load parameters\n",
    "inifile = configparser.ConfigParser()\n",
    "inifile.read(\"./config.ini\")\n",
    "size = int(inifile.get('cifar10','size'))\n",
    "num_classes = int(inifile.get('cifar10','num_classes'))\n",
    "\n",
    "## Memory setting\n",
    "config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))\n",
    "session = tf.Session(config=config)\n",
    "K.set_session(session)\n",
    "\n",
    "## Data preparation (use only test set)\n",
    "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n",
    "\n",
    "# y_train = np_utils.to_categorical(y_train, num_classes)\n",
    "# x_train = arr_resize(x_train, size)\n",
    "\n",
    "y_test = np_utils.to_categorical(y_test, num_classes)\n",
    "x_test = arr_resize(x_test, size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rescaling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_test = x_test / 255."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Evaluation: simple accuracy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9238\n",
      "CPU times: user 1min 4s, sys: 24.3 s, total: 1min 28s\n",
      "Wall time: 1min 26s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "model = SEResNeXt(size=size).model\n",
    "model.load_weights(\"trained_model/SEResNeXt.h5\", by_name=True)\n",
    "\n",
    "predictions = model.predict(x_test)\n",
    "\n",
    "prediction_idx = map( np.argmax, predictions )\n",
    "test_idx = map(np.argmax, y_test)\n",
    "\n",
    "correspondence = list( map( lambda x,y: x == y, list(prediction_idx), list(test_idx) ) )\n",
    "\n",
    "print( sum(correspondence) / len(correspondence) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
